package com.ruoyi.school.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.school.domain.SchoolTdx;
import com.ruoyi.school.domain.SchoolTdxDto;
import java.lang.reflect.Field;
import java.util.List;
import org.springframework.beans.BeanUtils;

/**
 * 投档线
 *
 * @author zyf
 * @date 2024-09-10
 */
public class TdxListener extends AnalysisEventListener<SchoolTdxDto> {

    private List<SchoolTdx> list;

    public TdxListener() {
    }

    public TdxListener(List<SchoolTdx> list) {
        this.list = list;
    }

    @Override
    public void invoke(SchoolTdxDto schoolTdx, AnalysisContext analysisContext) {
//        定义初始值，防止反射获取值为null
        SchoolTdx schoolTdx1 = new SchoolTdx(0L, 0L, 0L, 0L);
        BeanUtils.copyProperties(schoolTdx, schoolTdx1);
        valueOf(schoolTdx, schoolTdx1, "lowestRank", "lowestRank");
        valueOf(schoolTdx, schoolTdx1, "lowestScore", "lowestScore");
        valueOf(schoolTdx, schoolTdx1, "highestScore", "highestScore");
        valueOf(schoolTdx, schoolTdx1, "average", "average");
        valueOf(schoolTdx,schoolTdx1,"year","year");
        list.add(schoolTdx1);
    }

    public <T, N> void valueOf(T t, N n, String key1, String key2) {
        try {
            Field field = t.getClass().getDeclaredField(key1);
            // 设置字段可访问（如果它是私有的）
            field.setAccessible(true);
            String values = (String) field.get(t);
            if (!values.contains("-")) {
                Field declaredField = n.getClass().getDeclaredField(key2);
                // 设置字段可访问（如果它是私有的）
                declaredField.setAccessible(true);
                declaredField.set(n, Long.valueOf(values));
            }
        } catch (NoSuchFieldException | IllegalAccessException noSuchFieldException) {
            throw new RuntimeException("没有该字段类型！！！！！！！");
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
